package com.manger.common.office.sax.parse;

import com.manger.common.office.sax.utils.TargetClassParse;
import com.manger.common.office.sax.utils.UserTargetModel;
import com.manger.utils.ClassUtils;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SAXSheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {

    private Class<?> claz;

    private List<UserTargetModel> userTargetModelList ;

    private Map<String,UserTargetModel> fieldMap = new HashMap<>();

    private List<Object> result = new ArrayList<>();

    private UserTargetModel userTargetModel;

    private SAXSheetRowHandler saxSheetRowHandler ;

    private Object object;

    private String colKey;

    private int rowNum;

    private String sheetName;

    public SAXSheetHandler(String sheetName,Class<?> claz,SAXSheetRowHandler saxSheetRowHandler){
        this.sheetName = sheetName;
        this.claz = claz;
        userTargetModelList = TargetClassParse.parse(claz);
        result = new ArrayList<>();
        fieldMap = new HashMap<>();
        this.saxSheetRowHandler = saxSheetRowHandler;
    }

    @Override
    public void startRow(int rowNum) {
        this.rowNum = rowNum;
        try {
            object = claz.newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void endRow(int rowNum) {
        this.rowNum = rowNum;
        if(rowNum <= 0){
            return;
        }
        result.add(saxSheetRowHandler.row(sheetName,object,rowNum));
    }

    @Override
    public void cell(String cellReference, String formattedValue, XSSFComment comment) {
        this.colKey = cellReference.substring(0,cellReference.length() - String.valueOf(rowNum+1).length());
        if(rowNum == 0){//解析表头
            userTargetModel  = TargetClassParse.getUserTargetModel(formattedValue,userTargetModelList);
            if(userTargetModel != null){
                fieldMap.put(colKey,userTargetModel);
            }
        }else {
            userTargetModel = fieldMap.get(colKey);
            if(userTargetModel != null){
                userTargetModel.getField().setAccessible(true);
                try{
                    String val = getDicValByText(formattedValue,userTargetModel.getDicMap());
                    val = val == null ? formattedValue : val;
                    ClassUtils.setFieldValue(object,val,userTargetModel.getField());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    private String getDicValByText(String text,Map<String,String> dicMap){
        if(dicMap == null){
            return null;
        }
        for(String key: dicMap.keySet()){
            if(dicMap.get(key).equals(text)){
                return key;
            }
        }
        return null;
    }

    @Override
    public void endSheet() {
    }

    @Override
    public void headerFooter(String text, boolean isHeader, String tagName) {
        System.out.println(text + " : " + isHeader + " : " + tagName);
    }

    public List<?> get(){
        return result;
    }
}
